package com.huazai.interceptor;

import com.huazai.bean.po.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class UserInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        User userMessage = (User) session.getAttribute("userMessage");
        String requestURI = request.getRequestURI();

        if (requestURI.contains("editMyself") || requestURI.contains("changePassword")) {
            return userMessage != null;
        }

        if (requestURI.contains("signupGeneral")
                || requestURI.contains("toSignupPaper")
                || requestURI.contains("active")
                || requestURI.contains("toUserInforPaper")) {

            return true;
        } else {
            if (userMessage != null && userMessage.getAuthority().equals("super")) {
                return true;
            } else {
            /*
            如果有人尝试越界的的话，就给他直接踢出去！
             */
                session.removeAttribute("userMessage");
                response.sendRedirect(request.getContextPath() + "/door/welcome");
                return false;
            }
        }
    }
}
